package demo.ht.com.basequickadpater.activitys;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.app.TakePhotoFragmentActivity;
import com.jph.takephoto.app.TakePhotoImpl;
import com.jph.takephoto.model.InvokeParam;
import com.jph.takephoto.model.TContextWrap;
import com.jph.takephoto.model.TResult;
import com.jph.takephoto.permission.PermissionManager;
import com.jph.takephoto.permission.TakePhotoInvocationHandler;

import java.io.File;

import demo.ht.com.basequickadpater.R;
import demo.ht.com.basequickadpater.dialog.SwitchDialogFragment;

/**
 * @Package: demo.ht.com.basequickadpater.activitys
 * @ClassName: ImageActivity
 * @Author: szj
 * @CreateDate: 6/4/21 10:24 AM
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class ImageActivity  extends TakePhotoFragmentActivity {

    private InvokeParam invokeParam;
    private ImageView image;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);

        image = findViewById(R.id.image);

    }

    public void onButtonClick(View view) {
        View view1 = LayoutInflater.from(this).inflate(R.layout.item_image_dialog, null, false);
        SwitchDialogFragment dialog = new SwitchDialogFragment(view1, "选择", this);
        initDialog(view1,dialog);

        dialog.show(getSupportFragmentManager(),dialog.getTag());

        dialog.setLeftVisibility(true);

        dialog.setRightVisibility(true);

        dialog.setBottomShow(true);

        dialog.setDialogAnimation(true);

    }

    private void initDialog(View view,SwitchDialogFragment dialog) {
        Button bt_top = view.findViewById(R.id.bt_top);
        Button bt_bottom = view.findViewById(R.id.bt_bottom);

        File file=new File(Environment.getExternalStorageDirectory(), "/temp/"+System.currentTimeMillis() + ".jpg");
        if (!file.getParentFile().exists())file.getParentFile().mkdirs();
        Uri imageUri = Uri.fromFile(file);

        bt_bottom.setOnClickListener(v -> {
            //从相册选取图片
            takePhoto.onPickFromCapture(imageUri);
            dialog.dismiss();
        });

        bt_bottom.setOnClickListener(v -> {
            //拍照
            takePhoto.onPickFromGallery();
            dialog.dismiss();

        });

    }

    //成功
    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);
        Log.i("szj图片成功",result.getImage().getPath().trim());
//        RequestOptions options = new RequestOptions()
//                .placeholder(R.drawable.ic_arrow_back)//图片加载出来前，显示的图片
//                .fallback( R.drawable.ic_arrow_back) //url为空的时候,显示的图片
//                .error(R.drawable.ic_arrow_back);//图片加载失败后，显示的图片

//        Glide.with(this)
//                .load(result.getImage().getPath().trim())
//                .apply(options)
//                .into(image);
    }

    //错误
    @Override
    public void takeFail(TResult result, String msg) {
        super.takeFail(result, msg);
    }

    //结束
    @Override
    public void takeCancel() {
        super.takeCancel();
    }

    private  TakePhoto takePhoto;

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //以下代码为处理Android6.0、7.0动态权限所需
        PermissionManager.TPermissionType type= PermissionManager.onRequestPermissionsResult(requestCode,permissions,grantResults);
        PermissionManager.handlePermissionsResult(this,type,invokeParam,this);

    }

    @Override
    public PermissionManager.TPermissionType invoke(InvokeParam invokeParam) {
        PermissionManager.TPermissionType type=PermissionManager.checkPermission(TContextWrap.of(this),invokeParam.getMethod());
        if(PermissionManager.TPermissionType.WAIT.equals(type)){
            this.invokeParam=invokeParam;
        }
        return type;
    }

    /**
     *  获取TakePhoto实例
     * @return
     */
    public TakePhoto getTakePhoto(){
        if (takePhoto==null){
            takePhoto= (TakePhoto) TakePhotoInvocationHandler.of(this).bind(new TakePhotoImpl(this,this));
        }
        return takePhoto;
    }



}
